<template>
    <div>
        <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>电影管理</el-breadcrumb-item>
            <el-breadcrumb-item>电影列表</el-breadcrumb-item>
        </el-breadcrumb> -->

        <!-- <el-divider></el-divider> -->

        <el-form :inline="true" :model="form" class="demo-form-inline" @submit.native.prevent>
            <el-form-item label="电影名称">
                <el-input @keyup.native.enter="onSubmit" v-model="form.name" placeholder="输入影片名:模糊查询"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
        <!-- 分割线 -->
        <el-divider content-position="left">电影列表</el-divider>

        <!-- 电影表格 -->
        <el-table :data="moviesData.result" border style="width: 100%">
            <el-table-column label="封面" width="120">
                <template slot-scope="scope">
                    <img :src="scope.row.cover" alt="" width="80">
                </template>
            </el-table-column>
            <el-table-column prop="title" label="标题" width="130">
            </el-table-column>
            <el-table-column prop="star_actor" label="主演" width="300">
            </el-table-column>
            <el-table-column prop="showingon" label="上映时间" width="120">
            </el-table-column>
            <el-table-column label="时长" width="80">
                <template slot-scope="scope">
                    {{ scope.row.duration }} 分钟
                </template>
            </el-table-column>
            <el-table-column prop="type" label="所属类别">
                <template slot-scope="scope">
                    <div slot="reference" class="name-wrapper">
                        <el-tag v-if="scope.row.type" size="medium" type="danger">{{ scope.row.type }}</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="240">
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="info" icon="el-icon-user" circle></el-button>
                        <el-button type="success" icon="el-icon-picture-outline" circle></el-button>
                        <el-button type="warning" icon="el-icon-edit" circle
                            @click="$router.push(`/home/movie-update/${scope.row.id}`)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle @click="delMovie(scope.row.id)">
                        </el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination background layout="->, prev, pager, next, total, jumper" :total="moviesData.total"
            :page-size="moviesData.pagesize" :current-page="moviesData.page" style="margin:10px;"
            @current-change="pageChange">
        </el-pagination>
    </div>
</template>

<script>
export default {
    inject: ['reload'],
    data() {
        return {
            form: {
                name: '' // 模糊搜索值
            },
            inputValue: '',
            moviesData: {
                page: 1,
                pagesize: 3,
                total: 0
            }, //电影数据
        }
    },
    methods: {
        pageChange(page) {
            //分页事件
            if (this.inputValue) {
                let param = { name: this.inputValue, page: page, pagesize: this.moviesData.pagesize }
                this.$http.movieApi.queryByNameLike(param).then(res => {
                    this.moviesData = res.data.data;
                })
            } else {
                let param = { page: page, pagesize: this.moviesData.pagesize }
                this.$http.movieApi.queryAll(param).then(res => {
                    this.moviesData = res.data.data
                })
            }


        },
        onSubmit() {
            // 模糊查询事件
            if (!this.form.name.trim()) {
                this.reload()
            } else {
                this.$http.movieApi.queryByNameLike({ name: this.form.name, page: 1, pagesize: this.moviesData.pagesize }).then(res => {
                    this.inputValue = this.form.name;
                    this.moviesData = res.data.data;
                })
            }

        },

        delMovie(id) {
            // 删除事件
            this.$confirm('此操作将永久删除此项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.movieApi.delete({ id }).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        // this.reload()
                        // 刷新当前列表，整理参数发送请求

                        if (this.inputValue) {
                            let params = {
                                name: this.inputValue,
                                page: this.moviesData.page,
                                pagesize: this.moviesData.pagesize
                            }
                            this.$http.movieApi.queryByNameLike(params).then(res => {
                                this.inputValue = this.form.name;
                                this.moviesData = res.data.data;
                            })
                        } else {
                            let params = {
                                page: this.moviesData.page,
                                pagesize: this.moviesData.pagesize
                            }
                            this.$http.movieApi.queryAll(params).then(res => {
                                this.moviesData = res.data.data
                            })
                        }
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    },
    mounted() {
        // 页面初始化加载
        let param = { page: 1, pagesize: this.moviesData.pagesize }
        this.$http.movieApi.queryAll(param).then(res => {
            this.moviesData = res.data.data
        })
    }
}
</script>

<style lang="scss" scoped>

</style>